package com.le.basic.Sort;

/**
 * 插入排序
 * 思想:
 *  当前元素与前一个元素相比,小于则交换位置, 继续与前面元素相比
 */
public class Code_03_InsertSort {

    public static void insertSort(int[] arr){
        if (arr == null || arr.length < 2){
            return;
        }
        for (int i = 1; i < arr.length; i++) {
            for (int j = i - 1; j >= 0 ; j--) {
                if (arr[j + 1] < arr[j] ){
                    int temp = arr[j + 1];
                    arr[j + 1] = arr[j];
                    arr[j] = temp;
                }
            }
        }
    }

    public static void main(String[] args) {
        int testTime = 500000;
        int maxSize = 10;
        int maxValue = 100;
        boolean succeed = true;
        for (int i = 0; i < testTime; i++) {
            int[] array = Code_01_BubbleSort.generateRandomArray(maxSize, maxValue);
            int[] array1 = Code_01_BubbleSort.copyArray(array);
            int[] array2 = Code_01_BubbleSort.copyArray(array);
            insertSort(array1);
            Code_01_BubbleSort.comparator(array2);
            if (!Code_01_BubbleSort.isEqual(array1, array2)){
                succeed = false;
                break;
            }
        }
        System.out.println(succeed ? "nice" : "fuck");
//        int[] temp = Code_01_BubbleSort.generateRandomArray(maxSize,maxValue);
//        insertSort(temp);
//        Code_01_BubbleSort.printArray(temp);
    }
}
